//
//  FGFromTemplateView.m
//  lite
//
//  Created by feige on 2022/11/2.
//

#import "FGFromTemplateView.h"
#import "UIColor+FGChat.h"
#import "UIView+FGLayout.h"

@interface FGFromTemplateViewCell()
@property (nonatomic, strong) UIView *bgView;
@end

@implementation FGFromTemplateViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        // bgView
        _bgView = [UIView new];
        _bgView.layer.cornerRadius = 4;
        _bgView.layer.borderColor = [UIColor fg_colorWithString:@"#C8CBD3"].CGColor;
        _bgView.layer.borderWidth = 0.5f;
        [self addSubview:_bgView];
        
        // titleLabel
        _titleLabel = [UILabel new];
        _titleLabel.font = [UIFont systemFontOfSize:14];
        [_bgView addSubview:_titleLabel];
        
        // textField
        _textField = [UITextField new];
        _textField.borderStyle = UITextBorderStyleNone;
        [_bgView addSubview:_textField];
        
        _titleLabel.text = @"公司名";
        
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    _bgView.frame = CGRectMake(10, 10, self.mm_w - 20, 34);
    
    _titleLabel.mm_sizeToFit().mm_left(15).mm__centerY(_bgView.mm_h/2.0f);
    
    _textField.frame = CGRectMake(70, 0, _bgView.mm_w - 70, 34);
}

@end

@interface FGFromTemplateView()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIView *lineView;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIButton *submitButton;
@property (nonatomic, strong) id templateData;
@property (nonatomic, strong) NSArray *dataArray;
@end

@implementation FGFromTemplateView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        
        // titleLabel
        _titleLabel = [UILabel new];
        _titleLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
        [self addSubview:_titleLabel];
        
        // tableView
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.rowHeight = 44;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.dataSource = self;
        _tableView.delegate = self;
        [_tableView registerClass:FGFromTemplateViewCell.class forCellReuseIdentifier:@"FGFromTemplateViewCell"];
        [self addSubview:_tableView];
        
        // lineView
        _lineView = [UIView new];
        _lineView.backgroundColor = [UIColor fg_colorWithString:@"#EBF1F5"];
        [self addSubview:_lineView];
        
        // submitButton
        _submitButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_submitButton setTitle:@"提 交" forState:0];
        [_submitButton setTitleColor:[UIColor whiteColor] forState:0];
        _submitButton.titleLabel.font = [UIFont systemFontOfSize:14 weight:UIFontWeightMedium];
        _submitButton.backgroundColor = [UIColor fg_colorWithString:@"#558DFF"];
        _submitButton.layer.cornerRadius = 17;
        [_submitButton addTarget:self action:@selector(submitAction:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_submitButton];
        
        _titleLabel.text = @"请填写你的个人信息";
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    _titleLabel.mm_sizeToFit().mm_right(10).mm_left(10).mm_top(10);
    
    _lineView.frame = CGRectMake(10, 42, self.mm_w - 20, 0.5f);
    
    _tableView.frame = CGRectMake(0, 42, self.mm_w, self.mm_h - 42 - 64);
    
    _submitButton.frame = CGRectMake(18, self.mm_h - 49, self.mm_w - 36, 34);
}

#pragma mark - Response Event

- (void)submitAction:(UIButton *)sender
{
    if (self.isPreview) {
        return;
    }
    
}

#pragma mark - UITableViewDataSource & UITableViewDelegate

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    NSDictionary *data = self.dataArray[indexPath.row];
    
    FGFromTemplateViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FGFromTemplateViewCell" forIndexPath:indexPath];
    cell.titleLabel.text = data[@"fieldName"];
    cell.textField.userInteractionEnabled = !self.isPreview;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (self.isPreview) {
        return;
    }
}

#pragma mark - Rewrite Mehtod

- (void)fillData:(id)templateData {
    self.templateData = templateData;
    
    self.titleLabel.text = [self.templateData valueForKeyPath:@"data.prompt"];
    self.dataArray = [self.templateData valueForKeyPath:@"data.fileds"];
    [self.tableView reloadData];
}

/*
 {
     "isView":false,
     "id":5,
     "data":{
         "fileds":[
             {
                 "formId":1,
                 "id":399,
                 "isRequired":true,
                 "labelName":"Phone",
                 "fieldName":"电话",
                 "sort":0
             }
         ],
         "id":1,
         "isOpen":false,
         "prompt":"请填写一下信息？",
         "isResend":true
     },
     "children":null,
     "type":5,
     "tips":"表单模板",
     "name":"表单模板",
     "pid":0
 }
 */

@end
